package com.wen.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExcelListener extends AnalysisEventListener<UserData> {
    //一行一行读取excel内容。从第二行开始读取
    List<UserData> userData = null;

    public ExcelListener(List<UserData> userData) {
        this.userData = userData;
    }

    @Override
    public void invoke(UserData data, AnalysisContext context) {
        userData.add(data);
        System.out.println(data);
    }

    //读取之后执行
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        System.out.println("userData = " + userData);
        System.out.println("读取完毕");
        //设置excel文件路径和文件名称;
        String fileName="F:\\项目\\excel\\整合.xlsx";
        //调用方法实现写操作
        EasyExcel.write(fileName,UserData.class).sheet("用户信息")
                .doWrite(userData);
    }

    //读取表头内容
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println(headMap);
    }
}
